#
# To build it with cmake, you should register qml types explicitly by calling registerQuickFluxQmlTypes() in your main.cpp
# See examples/middleware for example
#

cmake_minimum_required(VERSION 3.0.0)
project(qamqp VERSION 1.1.3)

EXECUTE_PROCESS(COMMAND git describe --tags
        TIMEOUT 10
        OUTPUT_VARIABLE GIT_VERSION
        OUTPUT_STRIP_TRAILING_WHITESPACE
        )
MESSAGE(STATUS "building from git tag ${GIT_VERSION}")
add_definitions(-DGIT_VERSION=\"${GIT_VERSION}\")

option(QAMQP_INSTALL "Enable the installation of targets." ON)
set(SRC_DIR "${PROJECT_SOURCE_DIR}/src")

include(GNUInstallDirs)
find_package(QT NAMES Qt6 Qt5 COMPONENTS Core REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core Network Quick CONFIG REQUIRED)

set(QAMQP_PUBLIC_SOURCES
  ${SRC_DIR}/qamqpauthenticator.cpp
  ${SRC_DIR}/qamqpchannel.cpp
  ${SRC_DIR}/qamqpchannelhash.cpp
  ${SRC_DIR}/qamqpclient.cpp
  ${SRC_DIR}/qamqpexchange.cpp
  ${SRC_DIR}/qamqpframe.cpp
  ${SRC_DIR}/qamqpmessage.cpp
  ${SRC_DIR}/qamqpqueue.cpp
  ${SRC_DIR}/qamqptable.cpp
  ${SRC_DIR}/rabbitmqclient.cpp
)

set(QAMQP_PUBLIC_HEADERS
  ${SRC_DIR}/qamqpauthenticator.h
  ${SRC_DIR}/qamqpchannel.h
  ${SRC_DIR}/qamqpclient.h
  ${SRC_DIR}/qamqpexchange.h
  ${SRC_DIR}/qamqpglobal.h
  ${SRC_DIR}/qamqpmessage.h
  ${SRC_DIR}/qamqpqueue.h
  ${SRC_DIR}/qamqptable.h
  ${SRC_DIR}/rabbitmqclient.h
  )

source_group(include FILES
  ${QAMQP_PUBLIC_HEADERS}
  )

add_library(${PROJECT_NAME} STATIC
  ${QAMQP_PUBLIC_SOURCES}
  ${QAMQP_PUBLIC_HEADERS}
  ${moc}
  )

target_link_libraries(${PROJECT_NAME}
  PUBLIC
  Qt${QT_VERSION_MAJOR}::Core
  Qt${QT_VERSION_MAJOR}::Network
  Qt${QT_VERSION_MAJOR}::Quick
  )

target_include_directories(${PROJECT_NAME}
  PUBLIC
  "$<BUILD_INTERFACE:${SRC_DIR}>"
  "$<INSTALL_INTERFACE:include>"
  )

set_target_properties(${PROJECT_NAME} PROPERTIES
  AUTOMOC TRUE
  DEBUG_POSTFIX d
  )

if(QAMQP_INSTALL)

  install(TARGETS ${PROJECT_NAME}
    LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
    ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
    RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
    INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
    )

  install(FILES
    ${QAMQP_PUBLIC_HEADERS}
    DESTINATION include
    )

#  include(CMakePackageConfigHelpers)
#  write_basic_package_version_file(
#      ${CMAKE_BINARY_DIR}/cmake/qampqVersion.cmake
#      VERSION ${PROJECT_VERSION}
#      COMPATIBILITY SameMajorVersion
#  )

  # installation - build tree specific package config files
#  export(EXPORT qamqpTargets FILE ${CMAKE_BINARY_DIR}/qamqpTargets.cmake)
#  configure_file(${PROJECT_SOURCE_DIR}/qamqpConfig.cmake.in
#      ${CMAKE_BINARY_DIR}/qamqpConfig.cmake
#      COPYONLY
#  )

  # installation - relocatable package config files
#  configure_package_config_file(${PROJECT_SOURCE_DIR}/qamqpConfig.cmake.in
#                                ${CMAKE_CURRENT_BINARY_DIR}/cmake/qamqpConfig.cmake
#                                INSTALL_DESTINATION cmake
#  )

#  set(CONFIG_PACKAGE_LOCATION ${CMAKE_INSTALL_LIBDIR}/cmake/qamqp)

#   install(EXPORT qamqpTargets
#       FILE qamqpTargets.cmake
#       NAMESPACE QuickFlux::
#       DESTINATION ${CONFIG_PACKAGE_LOCATION}
#   )

#  install(FILES
#      ${CMAKE_BINARY_DIR}/cmake/qamqpConfig.cmake
#      ${CMAKE_BINARY_DIR}/cmake/qamqpVersion.cmake
#      DESTINATION ${CONFIG_PACKAGE_LOCATION}
#  )
endif()
